#pragma once
#include <glad/glad.h>

class Shader;

class MeshRenderer
{
public:
    void SetShader(Shader* shader) {
        this->shader = shader;
    }

    void SetVertex(float* vertex, int length) {
        this->vertex = vertex;
        this->vertexLength = length;
    }

    void SetIndices(uint32_t* indices, uint32_t length) {
        this->indices = indices;
        this->indicesLength = length;
    }

    void Init() {
        // VAO
        glGenVertexArrays(1, &vao);
        glBindVertexArray(vao);
        // VBO
        glGenBuffers(1, &vbo);
        glBindBuffer(GL_ARRAY_BUFFER, vbo);
        glBufferData(GL_ARRAY_BUFFER, static_cast<long long>(vertexLength * sizeof(float)), vertex, GL_STATIC_DRAW);
        // EBO
        glGenBuffers(1, &ebo);
        glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ebo);
        glBufferData(GL_ELEMENT_ARRAY_BUFFER, static_cast<long long>(indicesLength * sizeof(uint32_t)), indices, GL_STATIC_DRAW);
        // 设置顶点属性指针
        glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float), nullptr);
        glEnableVertexAttribArray(0);
    }

    void Draw() {
        shader->Use();
        glBindVertexArray(vao);
        glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ebo);
        glDrawElements(GL_TRIANGLES, static_cast<GLsizei>(indicesLength), GL_UNSIGNED_INT, nullptr);
    }
    MeshRenderer() {}
private:
    Shader* shader;
    // 顶点数组
    float* vertex;
    // 顶点数组的长度
    uint32_t vertexLength;

    // 索引数组
    uint32_t* indices;
    // 索引数组的长度
    uint32_t indicesLength;

    uint32_t vao;
    uint32_t ebo;
    uint32_t vbo;
};